#include "colorwidget.h"
#include "ui_colorwidget.h"
#include <QColorDialog>
#include <QDebug>
ColorWidget::ColorWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ColorWidget)
{
    ui->setupUi(this);
    setWindowFlags(Qt::Popup | Qt::FramelessWindowHint);// 设置窗口标志，使其表现为弹出窗口，并且无边框

    // 颜色块按钮的点击事件
    QList<QToolButton*> buttonList = ui->widget->findChildren<QToolButton*>();
    for (QToolButton* button : buttonList) {
        QObject::connect(button, &QToolButton::clicked, this, [this, button]() {
            QColor backgroundColor = button->palette().color(QPalette::Button);
            emit changeColor(backgroundColor);
            qDebug() << "row 15 in colorwidget, color is" << backgroundColor;
            if (ui->toolButton_1->isChecked()) {
                // 选中的“颜色1”按钮变色
                QString styleSheet = QString("background-color: %1;").arg(backgroundColor.name());
                ui->toolButton_1->setStyleSheet(styleSheet);

            } else if (ui->toolButton_2->isChecked()) {
                // 选中的“颜色2”按钮变色
                QString styleSheet = QString("background-color: %1;").arg(backgroundColor.name());
                ui->toolButton_2->setStyleSheet(styleSheet);
            }
        });
    }

    // 两个可选按钮“颜色1” 和 “颜色2” 的选中槽函数连接
    QObject::connect(ui->toolButton_1, &QToolButton::clicked, this, [this]() {
        QColor backgroundColor = ui->toolButton_1->palette().color(QPalette::Button);
        emit changeColor(backgroundColor);
    });
    QObject::connect(ui->toolButton_2, &QToolButton::clicked, this, [this]() {
        QColor backgroundColor = ui->toolButton_2->palette().color(QPalette::Button);
        emit changeColor(backgroundColor);
    });


}

ColorWidget::~ColorWidget()
{
    delete ui;
}

void ColorWidget::on_toolButton_3_clicked() //自定义选择颜色
{
    QColor col = QColorDialog::getColor(Qt::black, this, "颜色编辑器", QColorDialog::ShowAlphaChannel);
    emit changeColor(col);
    if (ui->toolButton_1->isChecked()) {
        // 选中的“颜色1”按钮变色
        QString styleSheet = QString("background-color: %1;").arg(col.name());
        ui->toolButton_1->setStyleSheet(styleSheet);

    } else if (ui->toolButton_2->isChecked()) {
        // 选中的“颜色2”按钮变色
        QString styleSheet = QString("background-color: %1;").arg(col.name());
        ui->toolButton_2->setStyleSheet(styleSheet);
    }

}
